package pl.makimo.ksr.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import pl.makimo.data.WeatherRecord;
import pl.makimo.fuzzy.sets.FuzzySet;
import pl.makimo.fuzzy.sets.memberships.MembershipFunction;

abstract public class FuzzySetCreator extends JFrame {
	public JPanel contentPane;
	public JPanel parametersPane;
	public JLabel membershipFunctionDescription;
	public MembershipFunction membershipFunction;
	public JButton okButton;
	public JTextField name;

	public JPanel checkboxPane;
	public JCheckBox relative = new JCheckBox();
	
	public String getName() {
		return name.getText();
	}
	
	abstract public FuzzySet getFuzzySet();
	
	public void setAction(ActionListener onSuccess) {
		okButton.addActionListener(onSuccess);
	}
	
	
	public void enableRelativeCheckbox() {
		checkboxPane.add(new JLabel("Relative?:"));
		checkboxPane.add(relative);		
	}	
	
	public JButton createFunctionMembershipButton(final Class<?> functionCreator, String title) {
		JButton b1 = new JButton(title);
	    b1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {				
				try {
					final MembershipFunctionCreator creator = (MembershipFunctionCreator)functionCreator.newInstance();
					creator.setAction(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							membershipFunction = creator.getFunction();
							membershipFunctionDescription.setText("Membership fn: " + membershipFunction.toString());
						}
					});
					creator.show();					
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});		
	    
	    return b1;
	}	
	
	/**
	 * Create the frame.
	 */
	public FuzzySetCreator() {
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		setContentPane(contentPane);
		
		name = new JTextField();
		name.setText("Name");
		contentPane.add(name);
		
		parametersPane = new JPanel();
		parametersPane.setLayout(new BoxLayout(parametersPane, BoxLayout.Y_AXIS));
		
		contentPane.add(parametersPane);
		
		membershipFunctionDescription = new JLabel();
		membershipFunctionDescription.setText("Membership fn: NONE");
		contentPane.add(membershipFunctionDescription);
		
		checkboxPane = new JPanel();
		checkboxPane.setLayout(new BoxLayout(checkboxPane, BoxLayout.Y_AXIS));		

		contentPane.add(checkboxPane);
		
		okButton = new JButton("OK");
		contentPane.add(okButton);
		
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
	}
}
